<template>
  <router-link :to="'/dashboard'" />
</template>

<script>
import Background from '@/assets/images/background.webp'
import qs from 'qs'
import { setToken } from '@/utils/auth'

export default {
  name: 'LoginSuccess',
  data() {
    return {
      Background: Background,
      token: '',
      user: '',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const data = route.query
        this.token = route.query.token
        this.user = JSON.parse(route.query.user)
        if (data && data.redirect) {
          this.redirect = data.redirect
          delete data.redirect
          if (JSON.stringify(data) !== '{}') {
            this.redirect = this.redirect + '&' + qs.stringify(data, { indices: false })
          }
        }
        console.log(route.query.token)
        console.log(JSON.parse(route.query.user))
      },
      immediate: true
    }

  },

  created() {
    // 设置Token
    this.setCookie()
    // 存储用户信息
    this.$store.dispatch('aEdit', this.user)
    this.$store.dispatch('aEdit2', this.token)
    // 跳转到主页
    this.$router.push({ path: '/dashboard' })
  },
  methods: {
    setCookie() {
      const access_token = this.token
      const rememberMe = false
      setToken(access_token, rememberMe)
    }
  }

}
</script>

<style scoped>

</style>
